package util.HtmlUnit;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;

public class SpiderContentxXML {

	public String getContent(String urlString, int SourceNet) {
		URL url = null;
		InputStream is = null;
		String content = new String();
		String asc = new String();
		System.setProperty("sun.net.client.defaultConnectTimeout", "15000");
		System.setProperty("sun.net.client.defaultReadTimeout", "15000");
		BufferedReader br = null;
		try {
			// System.out.println(urlString);
			url = new URL(urlString);
			/*
			 * InetSocketAddress isa = new InetSocketAddress("",0); Proxy proxy
			 * = new Proxy(Proxy.Type.HTTP,isa); is =
			 * url.openConnection(proxy).getInputStream();
			 */
			is = url.openStream();
			switch (SourceNet) {
			case 1:
				asc = "gbk";
				break;
			case 2:
				asc = "gbk";
				break;
			case 3:
				asc = "gbk";
			case 4:
				asc = "gbk";
				break;
			case 5:
				asc = "utf-8";
				break;
			case 6:
				asc = "gbk";
				break;
			default:
				System.out.println("default");
			}
			br = new BufferedReader(new InputStreamReader(is, asc));
			int i = -1;
			byte[] b = new byte[1024];
			StringBuffer sb = new StringBuffer();
			while ((i = is.read(b)) != -1) {
				sb.append(new String(b, 0, i,Charset.forName(asc)));
			}
			content = sb.toString();
		} catch (MalformedURLException e) {
			// e.printStackTrace();
			content = "";
		} catch (IOException e) {
			// e.printStackTrace();
			content = "";
		} finally {
			if (br != null) {
				try {
					br.close();
					br = null;
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (is != null) {
				try {
					is.close();
					is = null;
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return content;
	}

}
